package com.campus.help.controller.admin;

import com.campus.help.common.Result;
import com.campus.help.service.DashboardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理员仪表盘控制器
 *
 * @author campus-help
 * @since 2024-01-29
 */
@Slf4j
@RestController
@RequestMapping("/admin/dashboard")
public class AdminDashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 获取仪表盘统计数据
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getDashboardStats() {
        try {
            Map<String, Object> stats = dashboardService.getDashboardStats();
            return Result.success("获取统计数据成功", stats);
        } catch (Exception e) {
            log.error("获取仪表盘统计数据失败", e);
            return Result.error("获取统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取最近任务
     */
    @GetMapping("/recent-tasks")
    public Result<Object> getRecentTasks(@RequestParam(defaultValue = "10") Integer limit) {
        try {
            Object recentTasks = dashboardService.getRecentTasks(limit);
            return Result.success("获取最近任务成功", recentTasks);
        } catch (Exception e) {
            log.error("获取最近任务失败", e);
            return Result.error("获取最近任务失败: " + e.getMessage());
        }
    }

    /**
     * 获取系统通知
     */
    @GetMapping("/notifications")
    public Result<Object> getNotifications(@RequestParam(defaultValue = "10") Integer limit) {
        try {
            Object notifications = dashboardService.getNotifications(limit);
            return Result.success("获取通知成功", notifications);
        } catch (Exception e) {
            log.error("获取通知失败", e);
            return Result.error("获取通知失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务类型分布数据
     */
    @GetMapping("/task-distribution")
    public Result<Object> getTaskDistribution() {
        try {
            Object distribution = dashboardService.getTaskDistribution();
            return Result.success("获取任务分布成功", distribution);
        } catch (Exception e) {
            log.error("获取任务分布失败", e);
            return Result.error("获取任务分布失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户活跃度数据
     */
    @GetMapping("/user-activity")
    public Result<Object> getUserActivity(@RequestParam(defaultValue = "7") Integer days) {
        try {
            Object activity = dashboardService.getUserActivity(days);
            return Result.success("获取用户活跃度成功", activity);
        } catch (Exception e) {
            log.error("获取用户活跃度失败", e);
            return Result.error("获取用户活跃度失败: " + e.getMessage());
        }
    }
}
